package com.itheima.app.manager;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.sms.SmsTemplate;
import com.itheima.domain.db.*;
import com.itheima.service.db.*;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class SettingManager {

    @DubboReference
    private QuestionService questionService;

    @DubboReference
    private NotificationService notificationService;

    // 查询通用设置
    public ResponseEntity findSettingVo() {
        // 1.获取线程内userId
        User user = UserHolder.get();
        // 2.查询陌生人问题
        Question question = questionService.findByUserId(user.getId());
        // 3.查询推送通知
        Notification notification = notificationService.findByUserId(user.getId());
        // 4.封装vo
        SettingVo vo = new SettingVo();
        vo.setId(user.getId()); // 用户id
        vo.setPhone(user.getMobile()); // 用户手机号
        if (question != null) {
            vo.setStrangerQuestion(question.getStrangerQuestion()); // 陌生人问题...
        }
        if (notification != null) { // 推送通知
            vo.setGonggaoNotification(notification.getGonggaoNotification());
            vo.setPinglunNotification(notification.getPinglunNotification());
            vo.setLikeNotification(notification.getLikeNotification());
        }
        // 5.返回vo
        return ResponseEntity.ok(vo);
    }

    // 设置陌生人问题
    public void setQuestion(String content) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.根据userId查陌生人问题
        Question question = questionService.findByUserId(userId);
        // 3.判断处理
        if (question == null) {
            question = new Question();
            question.setUserId(userId);
            question.setStrangerQuestion(content);
            // 新增
            questionService.save(question);
        } else {
            question.setStrangerQuestion(content);
            // 修改
            questionService.update(question);

        }
    }

    // 设置推送通知
    public void setNotification(Notification param) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.查询推送通知
        Notification notification = notificationService.findByUserId(userId);
        // 3.判断
        if (notification == null) {
            param.setUserId(userId);
            // 新增
            notificationService.save(param);
        } else {
            notification.setGonggaoNotification(param.getGonggaoNotification());
            notification.setLikeNotification(param.getLikeNotification());
            notification.setPinglunNotification(param.getPinglunNotification());
            // 更新
            notificationService.update(notification);
        }
    }

    @DubboReference
    private BlackListService blackListService;

    @DubboReference
    private UserInfoService userInfoService;

    // 黑名单分页查询
    public ResponseEntity findBlackListByPage(Integer pageNum, Integer pageSize) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.根据条件查询黑名单分页对象
        PageBeanVo pageBeanVo = blackListService.findByPage(pageNum, pageSize, userId);
        // 3.查询userInfo
        // 3-1 获取黑名单集合
        List<BlackList> blackListList = (List<BlackList>) pageBeanVo.getItems();
        // 3-2 声明userInfo的集合
        List<UserInfo> userInfoList = new ArrayList<>();
        // 3-3 遍历黑名单集合
        if (blackListList != null && blackListList.size() > 0) {
            for (BlackList blackList : blackListList) {
                // 获取黑名单用户的id
                Long blackUserId = blackList.getBlackUserId();
                // 查询userInfo
                UserInfo userInfo = userInfoService.findByUserId(blackUserId);
                // 添加到集合
                userInfoList.add(userInfo);
            }
        }
        // 4.将userInfo封装到分页对象
        pageBeanVo.setItems(userInfoList);
        // 5.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    // 移除黑名单
    public void deleteBlackList(Long blackUserId) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc删除
        blackListService.delete(userId, blackUserId);
    }


    @DubboReference
    private UserService userService;

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //修改手机号- 1 发送短信验证码
    public void updatePhoneSendSms() {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        User user = userService.findByuserId(userId.toString());
        String mobile = user.getMobile();
        // 1.生成6位随机数
        String code = RandomUtil.randomNumbers(6);
        code = "123456";
        // 2.调用阿里云发送短信
        // smsTemplate.sendSms(mobile, code);
        // 3.将验证码存入redis
        stringRedisTemplate.opsForValue().set(ConstantUtil.SMS_CODE + mobile, code, Duration.ofMinutes(5));
    }

    //修改手机号 - 2 校验验证码
    public ResponseEntity verifyUpdatePhone(String verificationCode) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        User user = userService.findByuserId(userId.toString());
        String mobile = user.getMobile();
        // 取出redis中验证码
        String codeFromRedis = stringRedisTemplate.opsForValue().get(ConstantUtil.SMS_CODE + mobile);
        // 对比验证码
        Boolean verification;
        HashMap<String, Object> resultMap = new HashMap<>();
        if (!StrUtil.equals(verificationCode, codeFromRedis)) {
            verification = false;
            resultMap.put("verification", verification);
            return ResponseEntity.ok(resultMap);
        }
        verification = true;
        // 6.返回结果

        resultMap.put("verification", verification);

        return ResponseEntity.ok(resultMap);
    }

    //修改手机号 - 3 保存
    public void saveUpdatePhone(String mobile) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        User user = userService.findByuserId(userId.toString());
        user.setMobile(mobile);
        userService.update(user);

    }
}
